﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netra
{
    class TradeRoute
    {
        public class Element
        {
            public Dictionary<Merch, int> Deals;
            public Planet Planet;

            public Element(Planet planet,Dictionary<Merch,int> deals)
            {
                Planet=planet;
                Deals=deals;
            }
        }

        private List<Element> elements;
        public List<Element> Path
        {
            get
            { 
                return new List<Element>(elements);
            }
        }


        private int current;

        public int Length
        {
            get
            {
                return elements.Count;
            }
        }

        public TradeRoute()
        {
            elements=new List<Element>();
            current = 0;
        }

        public void Add(Planet planet, Dictionary<Merch,int> trades)
        {
            elements.Add(new Element(planet,trades));
        }

        /// <summary>
        /// Clear all path and deals
        /// </summary>
        public void Clear()
        {
            elements.Clear();
        }

        public IPosition Next()
        {
            if(elements.Count==0)
                throw new System.InvalidOperationException("Requested next element of empty trade route");
            if (current >= elements.Count - 1)
                current = 0;
            else
                current++;
            return elements[current].Planet;
        }

        public Dictionary<Merch, int> GetCurrentDeals()
        {
            return elements[current].Deals;
        }

        public HashSet<Merch> GetWaresInvolved()
        {
            var wares_involved = new HashSet<Merch>();

            foreach(Element el in elements)
                foreach (Merch ware in el.Deals.Keys)
                {
                    wares_involved.Add(ware);
                }
            return wares_involved;
        }
    }
}
